package com.stx.boke.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.jstl.sql.Result;

import com.stx.boke.base.dao.BaseDao;
import com.stx.boke.base.dao.jdbcOracleUtil;
import com.stx.boke.dao.UserDao;
import com.stx.boke.entity.Blogger;
import com.stx.boke.entity.User;

import com.stx.demo.util.JdbcUtil;

public class UserDaoImpl extends BaseDao implements UserDao{

	@Override
	public User getUser(User condition) {
		// TODO Auto-generated method stub
		User user =null;
		try {
			super.setConn(jdbcOracleUtil.getConnection());
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		super.setSql("select * from tbl_user  where uname=? and password=? ");
		List<Object> param = new ArrayList<Object>();
		param.add(condition.getUname());
		param.add(condition.getPassword());
		super.setParam(param);
		try {
			Result result = (Result) super.executeQuery();
			if (result != null && result.getRowCount() > 0) {
//				int usid =(Integer) result.getRows()[0].get("usid");
				String uname = (String) result.getRows()[0].get("uname");
				String upword = (String) result.getRows()[0].get("password");
				String blogname = (String )result.getRows()[0].get("blogname");

				

				user = new User();
				//user.setUsid(usid);
				user.setUname(uname);
				user.setPassword(upword);
				user.setBlogname(blogname);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public int addUser(User condition) {
		// TODO Auto-generated method stub
		int purk =0;
		try {
			super.setConn(jdbcOracleUtil.getConnection());
			super.setSql("insert into tbl_user(usid,uname,password," +
					"email,createtime,blogname)" +
					" values(usid.nextval,?,?,?,?,?)");
			List<Object> list = new ArrayList<Object>();
			

			
			list.add(condition.getUname());
			list.add(condition.getPassword());
			list.add(condition.getEmail());
			list.add(condition.getCreatetime());
			list.add(condition.getBlogname());
			super.setParam(list);
			purk = super.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return purk;
	}
	
	
	public int addBlogger(Blogger condition) {
		// TODO Auto-generated method stub
		int Bloggernumber =0;
		try {
			super.setConn(jdbcOracleUtil.getConnection());
			super.setSql("insert into tbl_blogger(bid,cid,content,title,usid)" +

					" values(bid.nextval,?,?,?,?)");
			List<Object> list = new ArrayList<Object>();
			

//			list.add(condition.getBid());
			list.add(condition.getCid());
			list.add(condition.getContent());
			list.add(condition.getTitle());
			list.add(condition.getUsid());
			
			super.setParam(list);
			Bloggernumber = super.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Bloggernumber;
	}

	@Override
	public List<Blogger> getBlogger(Blogger condition) throws SQLException {
		// TODO Auto-generated method stub

		// 获取connection对象
		Connection con = JdbcUtil.getConnection();

		Statement st = con.createStatement();

		ResultSet rs = st.executeQuery("select bid,title,content,cid from tbl_blogger");
		// 创建学生信息列表对象(用于存放从数据库查询出的多个学生信息)
		List<Blogger> bloList = new ArrayList<Blogger>();

		while (rs.next()) {
			// 创建学生数据视图对象
			Blogger blogger = new Blogger();

			// 将数据库记录的每一个字段的值 赋值给 "学生对象"对应的属性.
			// 即 学生对象.学号 <= 该条学生记录的学号
			blogger.setBid(rs.getInt("bid"));
			blogger.setCid(rs.getString("cid"));
			blogger.setContent(rs.getString("content"));
			blogger.setTitle(rs.getString("title"));

			// 将该学生对象 添入 学生信息列表对象
			bloList.add(blogger);
		}
		// 释放数据库连接资源(切勿忘记)
		JdbcUtil.release();

		// 返回学生信息列表对象(给servlet)
		return bloList;
		
		
		
		

			}



	}



